package com.accp.restaurant.service;


import com.accp.restaurant.dao.OrderdishesDao;
import com.accp.restaurant.entity.Orders;
import com.accp.restaurant.entity.Orderdishes;
import com.accp.restaurant.entity.Room;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service
public class RoomService {

    @PersistenceContext
    private EntityManager entityManager;

    @Autowired
    private OrderdishesDao orderdishesDao;


    public List<Room> querySearch(){
        String jpql = "select r from Room r join fetch r.roomtype where r.roomtype.roomtypeid between 1 and 7 group by r.roomtype.roomtypeid";
        Query query = entityManager.createQuery(jpql);
        return query.getResultList();
    }


    public List<Room> query(Integer pageNumber, Integer pageSize, Room room,Integer tabinput) {
        if (tabinput == 1) {
            String jpql = "select r from Room r join fetch r.roomtype where 1=1   and r.roomtype.roomtypeid between 4 and 6";
            long typeId = room.getRoomtype().getRoomtypeid();
            if (typeId != 10 && typeId != 0) {
                jpql = jpql + "  and r.roomtype.roomtypeid =:typeId";
            }
            long state = room.getRoomstate();
            if (state != 3 && state != 0) {
                jpql = jpql + " and r.roomstate =:state order by r.roomid";
            } else {
                jpql += " order by r.roomid";
            }
            Query query = entityManager.createQuery(jpql);
            if (room.getRoomtype().getRoomtypeid() != 10 && room.getRoomtype().getRoomtypeid() != 0) {
                query.setParameter("typeId", room.getRoomtype().getRoomtypeid());
            }
            if (room.getRoomstate() != 3 && room.getRoomstate() != 0) {
                query.setParameter("state", room.getRoomstate());
            }
            query.setFirstResult((pageNumber - 1) * pageSize);
            query.setMaxResults(pageSize);
            return query.getResultList();
        } else if (tabinput == 2) {
            String jpql = "select r from Room r join fetch r.roomtype where 1=1  and r.roomtype.roomtypeid between 1 and 3";
            long typeId = room.getRoomtype().getRoomtypeid();
            if (typeId != 10 && typeId != 0) {
                jpql = jpql + "  and r.roomtype.roomtypeid =:typeId";
            }
            long state = room.getRoomstate();
            if (state != 3 && state != 0) {
                jpql = jpql + " and r.roomstate =:state order by r.roomid";
            } else {
                jpql += " order by r.roomid";
            }
            Query query = entityManager.createQuery(jpql);
            if (room.getRoomtype().getRoomtypeid() != 10 && room.getRoomtype().getRoomtypeid() != 0) {
                query.setParameter("typeId", room.getRoomtype().getRoomtypeid());
            }
            if (room.getRoomstate() != 3 && room.getRoomstate() != 0) {
                query.setParameter("state", room.getRoomstate());
            }
            query.setFirstResult((pageNumber - 1) * pageSize);
            query.setMaxResults(pageSize);
            return query.getResultList();
        }
        return null;
    }

    public Long getCount(Integer pageNumber,Integer pageSize,Room room,Integer tabinput){
        if(tabinput==1){
            String jpql = "select count(r) from Room r inner join r.roomtype where 1=1 and r.roomtype.roomtypeid between 4 and 6";
            if(room.getRoomtype().getRoomtypeid()!=10&&room.getRoomtype().getRoomtypeid()!=0){
                jpql +=" and r.roomtype.roomtypeid = :typeId";
            }
            if(room.getRoomstate()!=3&&room.getRoomstate()!=0){
                jpql +=" and r.roomstate = :state";
            }
            Query query = entityManager.createQuery(jpql);
            if(room.getRoomtype().getRoomtypeid()!=10&&room.getRoomtype().getRoomtypeid()!=0){
                query.setParameter("typeId",room.getRoomtype().getRoomtypeid());
            }
            if(room.getRoomstate()!=3&&room.getRoomstate()!=0){
                query.setParameter("state",room.getRoomstate());
            }
            return (Long)query.getSingleResult();
        }else if(tabinput==2){
            String jpql = "select count(r) from Room r inner join r.roomtype where 1=1 and r.roomtype.roomtypeid between 1 and 3";
            if(room.getRoomtype().getRoomtypeid()!=10&&room.getRoomtype().getRoomtypeid()!=0){
                jpql +=" and r.roomtype.roomtypeid = :typeId";
            }
            if(room.getRoomstate()!=3&&room.getRoomstate()!=0){
                jpql +=" and r.roomstate = :state";
            }
            Query query = entityManager.createQuery(jpql);
            if(room.getRoomtype().getRoomtypeid()!=10&&room.getRoomtype().getRoomtypeid()!=0){
                query.setParameter("typeId",room.getRoomtype().getRoomtypeid());
            }
            if(room.getRoomstate()!=3&&room.getRoomstate()!=0){
                query.setParameter("state",room.getRoomstate());
            }
            return (Long)query.getSingleResult();
        }
        return null;
    }

        public List<Orderdishes> UpOrderdishes(int roomId,int dishesId,String type){
            String jpql="select * from orderdishes od inner join room r on od.roomId=r.roomId inner join dishes ds on od.dishesId= ds.dishesId" +
                    "where r.roomId=roomId";
            Orderdishes orderdishes=(Orderdishes) entityManager.createQuery(jpql);
            System.out.println(orderdishes.getRoom().getRoomname());
            return null;

        }

}
